﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmDuyetNhapHang : Form
    {
        private BangKeNhapHangMgt bangkenhaphangMgt = new BangKeNhapHangMgt();
        private MuaHangManagement muahangMgt = new MuaHangManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmDuyetNhapHang()
        {
            InitializeComponent();
        }

        private void FrmDuyetNhapHang_Load(object sender, EventArgs e)
        {
            dgvDanhSachBK.Columns["colNgayThang"].DefaultCellStyle.Format = "dd/MM/yyyy";
            dgvDanhSachBK.AutoGenerateColumns = false;
            dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();

            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            List<BangKeNhapHang> lstBangKeNhapHang = (from bk in GetAllNewBangKeNhapHang()
                                                      where bk.NgayThang.Date >= dtTuNgay.Date && bk.NgayThang.Date <= dtDenNgay.Date &&
                                                      bk.IsDelete==0
                                                      select bk).ToList();
            dgvDanhSachBK.DataSource = lstBangKeNhapHang;
        }        

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBK.CurrentRow != null)
            {
                if (dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString().Substring(0, 2) == "BK")
                {
                    BangKeNhapHang bangkenhaphang = new BangKeNhapHang();

                    DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;
                    bangkenhaphang.Id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());

                    FrmXemReport frmView = new FrmXemReport(bangkenhaphang);
                    frmView.Show();
                }
                else if (dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString().Substring(0, 2) == "MH")
                {
                    //MuaHang muahang = new MuaHang();

                    //DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;
                    //muahang.Id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());

                    //FrmXemReport frmView = new FrmXemReport(muahang);
                    //frmView.Show();
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn một bảng kê !");
            }
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBK.CurrentRow != null)
            {
                if (dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString().Substring(0, 2) == "BK")
                {
                    btnRefresh.BackColor = Color.Empty;

                    BangKeNhapHang bangkenhaphang = new BangKeNhapHang();

                    List<BangKeNhapHang> lstBangKeNhapHang = new List<BangKeNhapHang>();
                    DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;

                    int id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());
                    string ma = dgvRow.Cells[1].Value.ToString();
                    lstBangKeNhapHang = GetAllNewBangKeNhapHang();

                    for (int i = 0; i < lstBangKeNhapHang.Count; i++)
                    {
                        if (Convert.ToInt32(lstBangKeNhapHang[i].Id.ToString()) == id && lstBangKeNhapHang[i].MaSo.ToString() == ma)
                        {
                            bangkenhaphang = lstBangKeNhapHang[i];
                            bangkenhaphang.KeToanTruong = this.nhanvien.HoTen;
                        }
                    }

                    this.Hide();

                    //frmPhieuNhapKho.formClosePhieuNhapKho += new FormClosePhieuNhapKhoEventHandler(frmPhieuNhapKho_formClosePhieuNhapKho);
                    FrmPhieuNhapKho frmPhieuNhapKho = new FrmPhieuNhapKho(bangkenhaphang, nhanvien);
                    // frmPhieuNhapKho.BangKeNhapHang = bangkenhaphang;
                    frmPhieuNhapKho.NhanVien = this.nhanvien;
                    frmPhieuNhapKho.ShowDialog();

                    this.Show();

                    dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();
                }
                else
                {
                    btnRefresh.BackColor = Color.Empty;

                    MuaHang muahang = new MuaHang();

                    List<MuaHang> lstMuaHang = new List<MuaHang>();
                    DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;

                    int id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());
                    string ma = dgvRow.Cells[1].Value.ToString();

                    lstMuaHang = GetAllMuaHang();

                    for (int i = 0; i < lstMuaHang.Count; i++)
                    {
                        if (Convert.ToInt32(lstMuaHang[i].Id.ToString()) == id && lstMuaHang[i].MaSo.ToString() == ma)
                        {
                            muahang = lstMuaHang[i];
                            muahang.NguoiLap = this.nhanvien.HoTen;
                        }
                    }

                    this.Hide();

                    FrmPhieuNhapKho frmPhieuNhapKho = new FrmPhieuNhapKho(muahang, nhanvien);
                    frmPhieuNhapKho.NhanVien = this.nhanvien;
                    frmPhieuNhapKho.ShowDialog();

                    this.Show();

                    dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn một bảng kê !");
            }
        }

        public void frmPhieuNhapKho_formClosePhieuNhapKho(BangKeNhapHang bangkenhaphang, bool IsDuyet)
        {
            if (IsDuyet)
            {
                bangkenhaphangMgt.UpdateBangKeNhapHang(bangkenhaphang);
                dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();
            }
        }

        public void frmPhieuNhapKho_formCloseNhapHang(BangKeNhapHang bangkenhaphang, bool IsDuyet)
        {
            if (IsDuyet)
            {
                bangkenhaphangMgt.UpdateBangKeNhapHang(bangkenhaphang);

                FrmPhieuNhapKho frmPhieuNhapKho = new FrmPhieuNhapKho(bangkenhaphang,nhanvien);
                frmPhieuNhapKho.BangKeNhapHang = bangkenhaphang;
                frmPhieuNhapKho.NhanVien = this.nhanvien;
                frmPhieuNhapKho.Show();

                dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();
            }
        }

        public List<BangKeNhapHang> GetAllNewBangKeNhapHang()
        {
            ResultObj resultObj = new ResultObj();

            List<BangKeNhapHang> lstBangKeNhapHangTemp = new List<BangKeNhapHang>();
            List<BangKeNhapHang> lstBangKeNhapHang = new List<BangKeNhapHang>();

            resultObj = bangkenhaphangMgt.GetAllBangKeNhapHang();
            lstBangKeNhapHangTemp = (List<BangKeNhapHang>)resultObj.Result;

            for (int i = 0; i < lstBangKeNhapHangTemp.Count; i++)
            {
                if (lstBangKeNhapHangTemp[i].KeToanTruong == "")
                {
                    lstBangKeNhapHang.Add(lstBangKeNhapHangTemp[i]);
                }
            }
            return lstBangKeNhapHang;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBK.CurrentRow != null)
            {
                if (dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString().Substring(0, 2) == "BK")
                {
                    BangKeNhapHang bangkenhaphang = new BangKeNhapHang();
                    List<BangKeNhapHang> lstBangKeNhapHang = new List<BangKeNhapHang>();
                    DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;

                    int id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());
                    string ma = dgvRow.Cells[1].Value.ToString();
                    lstBangKeNhapHang = GetAllNewBangKeNhapHang();

                    for (int i = 0; i < lstBangKeNhapHang.Count; i++)
                    {
                        if (Convert.ToInt32(lstBangKeNhapHang[i].Id.ToString()) == id && lstBangKeNhapHang[i].MaSo.ToString() == ma)
                        {
                            bangkenhaphang = lstBangKeNhapHang[i];
                        }
                    }

                    FrmNhapHang frmNhapHang = new FrmNhapHang(bangkenhaphang, this.nhanvien, true);
                    frmNhapHang.formClosePhieuNhapKho += new FormClosePhieuNhapKhoEventHandler(frmPhieuNhapKho_formCloseNhapHang);
                    frmNhapHang.Show();
                }
                else if (dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString().Substring(0, 2) == "MH")
                {
                    MuaHang muahang = new MuaHang();
                    List<MuaHang> lstMuahang = new List<MuaHang>();
                    DataGridViewRow dgvRow = dgvDanhSachBK.CurrentRow;

                    int id = Convert.ToInt32(dgvRow.Cells[0].Value.ToString());
                    string ma = dgvRow.Cells[1].Value.ToString();
                    lstMuahang = GetAllMuaHang();

                    for (int i = 0; i < lstMuahang.Count; i++)
                    {
                        if (Convert.ToInt32(lstMuahang[i].Id.ToString()) == id && lstMuahang[i].MaSo.ToString()==ma)
                        {
                            muahang = lstMuahang[i];
                        }
                    }

                    FrmMuaHang frmMuaHang = new FrmMuaHang(muahang, this.nhanvien);
                    frmMuaHang.Show();
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn một bảng kê !");
            }
        }

        public void frmPhieuNhapKho_formCloseNhapHang()
        {

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            dgvDanhSachBK.DataSource = GetAllNewBangKeNhapHang();
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            List<BangKeNhapHang> lstBangKeNhapHang = (from bk in GetAllNewBangKeNhapHang()
                                                      where (bk.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NhaCungCap.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NguoiGiaoHang.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                      bk.NgayThang.Date >= dtTuNgay.Date && bk.NgayThang.Date <= dtDenNgay.Date &&
                                                      bk.IsDelete==0
                                                      select bk).ToList();
            dgvDanhSachBK.DataSource = lstBangKeNhapHang;
            btnRefresh.BackColor = Color.GreenYellow;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            List<BangKeNhapHang> lstBangKeNhapHang = (from bk in GetAllNewBangKeNhapHang()
                                                      where (bk.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NhaCungCap.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NguoiGiaoHang.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                      bk.NgayThang.Date >= dtTuNgay.Date && bk.NgayThang.Date <= dtDenNgay.Date &&
                                                      bk.IsDelete==0
                                                      select bk).ToList();
            List<MuaHang> lstMuaHang = (from bk in GetAllMuaHang()
                                                      where (bk.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NhaCungCap.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                      bk.NguoiGiaoHang.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                      bk.NgayThang.Date >= dtTuNgay.Date && bk.NgayThang.Date <= dtDenNgay.Date &&
                                                      bk.IsDelete == 0
                                                      select bk).ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("MaSo");
            dt.Columns.Add("NgayThang");
            dt.Columns.Add("NhaCungCap");
            dt.Columns.Add("NguoiGiaoHang");

            if (lstBangKeNhapHang.Count > 0)
            {
                foreach (BangKeNhapHang bk in lstBangKeNhapHang)
                {
                    DataRow row = dt.NewRow();
                    row[0] = bk.Id;
                    row[1] = bk.MaSo;
                    row[2] = bk.NgayThang;
                    row[3] = bk.NhaCungCap;
                    row[4] = bk.NguoiGiaoHang;
                    dt.Rows.Add(row);
                }
            }

            if (lstMuaHang.Count > 0)
            {
                foreach (MuaHang mh in lstMuaHang)
                {
                    DataRow row = dt.NewRow();
                    row[0] = mh.Id;
                    row[1] = mh.MaSo;
                    row[2] = mh.NgayThang;
                    row[3] = mh.NhaCungCap;
                    row[4] = mh.NguoiGiaoHang;
                    dt.Rows.Add(row);
                }
            }

            dgvDanhSachBK.DataSource = dt;
        }

        private void dgvDanhSachBK_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string ma = dgvDanhSachBK.CurrentRow.Cells[1].Value.ToString();
            if (ma.Substring(0,2) == "BK")
            {
                BangKeNhapHang bangkenhaphang = (from bk in GetAllNewBangKeNhapHang()
                                                where bk.MaSo == ma
                                                select bk).FirstOrDefault();
                FrmKiemTraPhieuNhapKho frmKTPhieuNhapKho = new FrmKiemTraPhieuNhapKho();
                frmKTPhieuNhapKho.BangKeNhapHang = bangkenhaphang;
                frmKTPhieuNhapKho.Show();
            }
            else if(ma.Substring(0,2) =="MH")
            {
                MuaHang muahang = (from mh in GetAllMuaHang()
                                  where mh.MaSo == ma
                                  select mh).FirstOrDefault();
                FrmMuaHang frmMuaHang = new FrmMuaHang(muahang, nhanvien);
                this.Hide();
                frmMuaHang.ShowDialog();
                this.Show();
            }
        }

        public List<MuaHang> GetAllMuaHang()
        {
            ResultObj obj = new ResultObj();

            obj = muahangMgt.GetAllMuaHang();
            List<MuaHang> listHangHoa = (List<MuaHang>)obj.Result;

            return listHangHoa;
        }
    }
}
